<template>
	<view class="index">
		<mescroll-uni height="100%" bottom="130" @init="mescrollInit" :up="upOption" :down="downOption">
			<view class="product-info-wrapper">
				<view class="supplier-wrapper">
					<view class="name">{{ form.supplierName }}</view>
					<uni-icons type="phone" size="18" class="icon" color="#666666"></uni-icons>
					<text class="tel">{{ form.supplierTel }}</text>
				</view>
				<view class="product-info">
					<view class="productName-wrapper">
						<view class="productName">
							{{ form.productName }}
						</view>
						<uni-icons type="right" color="#ffffff" size="18"></uni-icons>
					</view>
					<view class="info-wrapper">
						<view class="item">
							货号：{{ form.productCode }}
						</view>
						<view class="item">
							规格：{{ form.specification || '-' }}
						</view>
						<view class="item">
							毛重：{{ form.grossWeight }}斤
						</view>
						<view class="item">
							净重：{{ form.netWeight }}斤
						</view>
						<view class="item" v-for="(item,index) in form.detailAttribute" :key="index">
							{{ item.name }}：{{ item.value }}
						</view>
					</view>
				</view>
			</view>
			<view class="reject-type" style="margin-bottom: 20rpx;">
				<view class="item">
					<text>商品总数量</text>
					<input type="number" class="input" disabled v-model="form.quantity" />
					<text>件</text>
				</view>
			</view>
			<view class="reject-type" style="margin-bottom: 20rpx;">
				<view class="item">
					<text>已质检数量</text>
					<input type="number" class="input" v-model="testingNumber" placeholder="请输入" />
					<text>件</text>
				</view>
			</view>
			<view class="reject-type">
				<view class="item" @click="rejectType = 2">
					<uni-icons v-if="rejectType !== 2" type="circle" size="24" color="#888888"></uni-icons>
					<uni-icons v-else type="checkbox-filled" size="24" color="#555BFF"></uni-icons>
					<text>全部合格</text>
				</view>
				<view class="item" @click="rejectType = 0">
					<uni-icons v-if="rejectType !== 0" type="circle" size="24" color="#888888"></uni-icons>
					<uni-icons v-else type="checkbox-filled" size="24" color="#555BFF"></uni-icons>
					<text>全部拒收</text>
				</view>
				<view class="item" @click="rejectType = 1">
					<uni-icons v-if="rejectType !== 1" type="circle" size="24" color="#888888"></uni-icons>
					<uni-icons v-else type="checkbox-filled" size="24" color="#555BFF"></uni-icons>
					<text>部分拒收</text>
				</view>
			</view>

			<view class="customer-wrapper" v-if="rejectType == 1">
				<view class="customer-item" v-for="(item,index) in customerList" :key="index">
					<view class="city">
						<text class="label">城市仓</text>
						<text class="value">{{ item.cityPointCode }}:{{ item.cityPointName }}</text>
					</view>
					<view class="item" v-for="(items,indexs) in item.list" :key="items.sortCode">
						<view class="customer">
							<text>{{ indexs + 1 }}、</text>
							<view class="customerName">【{{ items.sortCode }}】{{ items.name }}</view>
						</view>
						<view class="number">
							<text>订货件数：</text>
							<!-- <input type="number" v-model="items.rejectQuantity" class="input" placeholder="拒收件数" /> -->
							<text>{{ items.quantity }}件</text>
						</view>
						<view class="number">
							<text>拒收件数：</text>
							<input type="number" v-model="items.rejectQuantity" class="input" placeholder="拒收件数" />
							<text>件</text>
						</view>

					</view>
				</view>
			</view>

			<view class="images-wrapper" v-if="rejectType !== 2">
				<view class="title-wrapper">
					<view class="title">
						相关记录(对不合格商品进行拍摄)
					</view>
				</view>
				<view class="product-image-list">
					<view class="item" v-for="(item,index) in imagelist" :key="index">
						<image @click="openPic(index)" :src="item+ '?imageView2/3/w/300/q/100'" class="product-image"
							mode="aspectFill">
						</image>
						<view class="del-wrapper" @click="delImage(index)">
							<uni-icons type="trash" color="#ffffff" size="20"></uni-icons>
						</view>
					</view>
					<view class="item add-item" @click="uploadByImage">
						<uni-icons type="plusempty" size="40" color="#A2A6A3"></uni-icons>
					</view>
				</view>
			</view>
			<view class="block"></view>
		</mescroll-uni>
		<view class="btn-wrapper">
			<view class="btn default" @click="goReject">
				质检记录
			</view>
			<view class="btn" v-if="$permissions.verify('app:rejects:add')" @click="submit">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		props: {
			code: {
				type: String,
				default () {
					return ''
				}
			},
		},
		data() {
			return {
				downOption: {
					use: false
				},
				upOption: {
					use: false,
					isLock: true
				},
				form: {},
				queryCode: '',
				rejectType: 2,
				customerList: [],
				dialog: false,
				imagelist: [],
				disabled: false,
				supplyOrderId: '',
				testingNumber: ''
			}
		},
		mounted() {
			this.queryCode = this.code;
			this.getData()
		},
		methods: {
			async getData() {
				const data = await this.$api.post('/adm/supply-reject/getRejectProduct', {
					orderDetailsId: this.queryCode
				}, false)
				if (data.code === 200) {
					this.supplyOrderId = data.data.supplyOrderId;
					if (this.form.productCode && this.form.productCode !== data.data.productCode) {
						this.dialog = true;
						this.$showModal({
							title: '覆盖确认',
							content: '当前已有操作中的货品，是否覆盖？',
							confirmText: '覆盖',
							cancelText: '取消',
							success: async (res) => {
								this.dialog = false;
								if (res.confirm) {
									this.form = {};
									this.setData(data.data)
								}
							}
						})
					} else {
						this.setData(data.data)
					}
				}
			},
			setData(data) {
				const cityPointCode = `${data.pickupPointCode}`
				console.log(cityPointCode)
				const customer = {
					name: data.customerPetName,
					customerId: data.customerId,
					quantity: data.quantity,
					rejectQuantity: 1,
					sortCode: data.sortCode,
					orderDetailsId: this.queryCode
				}
				const customerObj = {
					cityPointName: `${data.cityName || ''}-${data.pickupPointName}`,
					cityPointCode: cityPointCode,
					pickupPointId: data.pickupPointId,
					pickupPointCode: data.pickupPointCode,
					pickupPointName: data.pickupPointName,
					cityCode: data.cityCode,
					cityId: data.cityId,
					cityName: data.cityName,
					list: [customer]
				}
				if (!this.form.customerId) {
					if (data.detailAttribute) {
						data.detailAttribute = JSON.parse(data.detailAttribute)
					} else {
						data.detailAttribute = [];
					}
					this.form = data;
					this.customerList = [customerObj];
				} else {
					const customerCityIndex = this.customerList.findIndex(item => {
						return item.cityPointCode === cityPointCode
					})
					if (customerCityIndex === -1) {
						this.customerList.push(customerObj)
					} else {
						const list = this.customerList[customerCityIndex].list;
						const customerIndex = list.findIndex(row => row.customerId === data.customerId)
						if (customerIndex > -1) {
							list[customerIndex].rejectQuantity = parseInt(list[customerIndex].rejectQuantity) +
								1;
						} else {
							list.push(customer)
						}
					}
				}
			},
			scanCode(code) {
				if (this.dialog) {
					return
				}
				this.queryCode = code;
				this.getData()
			},
			openPic(index) {
				uni.previewImage({
					urls: this.imagelist,
					current: index
				});
			},
			delImage(index) {
				this.imagelist.splice(index, 1)
			},
			async uploadByImage() {
				let _this = this;
				uni.chooseImage({
					count: 9 - _this.imagelist.length, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera'], //从相册选择
					success: async (res) => {
						let paths = [];
						for (const [index, item] of res.tempFilePaths.entries()) {
							const url = await _this.$uploadFn(item, 0, 'reject', 0, true);
							paths.push({
								index,
								url
							});
						}
						paths.sort((a, b) => a.index - b.index);
						for (const obj of paths) {
							_this.imagelist.push(obj.url);
						}
					}
				})
			},
			goReject() {
				uni.navigateTo({
					url: '/subPage_testing/index'
				})
			},
			async submit() {
				if (this.disabled) return
				let rejectType = "";
				let rejectQuantity = 0;
				if (this.rejectType === 0) {
					rejectType = 'ALL'
				} else if (this.rejectType === 1) {
					rejectType = 'PART'
				} else if (this.rejectType === 2) {
					rejectType = 'PASS'
				}
				//const rejectType = this.rejectType === 0 ? 'ALL' : 'PART';
				if (!this.testingNumber) {
					uni.showToast({
						title: "请输入已质检数量",
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (parseInt(this.testingNumber) > parseInt(this.form.quantity)) {
					uni.showToast({
						title: "已质检数量不能大于商品数量",
						icon: 'none',
						duration: 2000
					})
					return
				}
				const params = {
					checkQuantity: this.testingNumber,
					supplyOrderId: this.supplyOrderId,
					productCode: this.form.productCode,
					productId: this.form.productId,
					productName: this.form.productName,
					rejectImage: this.imagelist.join(),
					rejectType: rejectType,
					supplierCode: this.form.supplierCode,
					supplierId: this.form.supplierId,
					supplierName: this.form.supplierName,
					supplierTel: this.form.supplierTel,
					supplyWarehouseCode: this.form.supplyWarehouseCode,
					supplyWarehouseId: this.form.supplyWarehouseId,
					supplyWarehouseName: this.form.supplyWarehouseName,
					supplyDate: this.form.supplyDate,
					quantity: this.form.quantity
				}
				let msg = "";
				if (rejectType === 'PART') {
					const rejectItems = [];
					this.customerList.forEach(item => {
						item.list.forEach(row => {
							if (!msg && (!row.rejectQuantity || row.rejectQuantity < 0)) {
								msg = "请填写正确的拒收数量";
							}
							if (!msg && parseInt(row.rejectQuantity) > parseInt(row.quantity)) {
								msg = "拒收数量不能大于订货量";
							}
							rejectItems.push({
								customerId: row.customerId,
								customerPetName: row.name,
								pickupPointCode: item.pickupPointCode,
								pickupPointId: item.pickupPointId,
								pickupPointName: item.pickupPointName,
								rejectQuantity: row.rejectQuantity,
								sortCode: row.sortCode,
								cityCode: item.cityCode,
								cityId: item.cityCode,
								cityName: item.cityName,
								orderDetailsId: row.orderDetailsId
							})
							rejectQuantity += parseInt(row.rejectQuantity);
						})
					})
					params.rejectItems = rejectItems;
				} else if (rejectType === 'ALL') {
					rejectQuantity = this.testingNumber;
				} else {
					params.rejectImage = "";
				}
				params.rejectQuantity = rejectQuantity;
				if (msg) {
					uni.showToast({
						title: msg,
						icon: 'none',
						duration: 2000
					})
					return
				}
				this.disabled = true;
				const data = await this.$api.post('/adm/supply-reject', params)
				if (data.code === 200) {
					var pages = getCurrentPages();
					const preRoute = pages[pages.length - 2].route; //获取当前页面路由
					if (preRoute !== 'subPage_testing/index') {
						uni.redirectTo({
							url: '/subPage_testing/index'
						})
					} else {
						pages[pages.length - 2].$vm.downCallback()
						uni.navigateBack()
					}
				} else {
					this.disabled = false;
				}
			}
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.index {
		height: 100%;
		overflow: hidden;

		.product-info-wrapper {
			margin: 20upx;
			border-radius: 20upx;
			background-color: #ffffff;
			overflow: hidden;

			.supplier-wrapper {
				height: 80upx;
				display: flex;
				align-items: center;
				padding: 0 30upx;
				background-color: #F5F6FA;

				.name {
					flex: 1;
					font-size: 32upx;
					//width: 500rpx;
					text-overflow: -o-ellipsis-lastline;
					overflow: hidden; //溢出内容隐藏
					text-overflow: ellipsis; //文本溢出部分用省略号表示
					display: -webkit-box; //特别显示模式
					-webkit-line-clamp: 1; //行数
					line-clamp: 1;
					-webkit-box-orient: vertical; //盒子中内容竖直排列
					font-weight: bold;
					color: #343434;
				}

				.tel {
					font-size: 32upx;
					color: #343434;
				}
			}

			.product-info {
				overflow: hidden;
				padding: 0 30upx;

				.productName-wrapper {
					margin-top: 20upx;
					display: flex;
					align-items: center;

					.productName {
						flex: 1;
						font-size: 32upx;
						//width: 500rpx;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden; //溢出内容隐藏
						text-overflow: ellipsis; //文本溢出部分用省略号表示
						display: -webkit-box; //特别显示模式
						-webkit-line-clamp: 1; //行数
						line-clamp: 1;
						-webkit-box-orient: vertical; //盒子中内容竖直排列
						font-weight: bold;
						color: #464646;
					}
				}

				.info-wrapper {
					display: flex;
					flex-wrap: wrap;
					padding-bottom: 20upx;

					.item {
						width: 50%;
						margin-top: 20upx;
						font-size: 30upx;
						color: #666666;
					}
				}
			}
		}

		.reject-type {
			display: flex;
			align-items: center;
			background-color: #ffffff;
			border-radius: 20upx;
			height: 90upx;
			padding: 0 30upx;
			margin: 0 20upx;

			.item {
				margin-right: 40upx;
				font-size: 34upx;
				display: flex;
				align-items: center;

				&:last-child {
					margin-right: 0;
				}

				.input {
					padding: 0 30rpx;
				}
			}
		}

		.customer-wrapper {
			margin: 20upx;
			background-color: #ffffff;
			border-radius: 20upx;
			padding: 30upx;
			padding-top: 0;

			.customer-item {
				&:first-child {
					.city {
						border-top: 0;
					}
				}

				.city {
					height: 90upx;
					display: flex;
					align-items: center;
					border-top: 1upx dashed #cecece;

					.label {
						margin-right: 20upx;
					}

					.value {
						font-weight: bold;
					}
				}

				.item {
					border-top: 1upx dashed #cecece;
					padding: 20upx 0;

					.customer {
						display: flex;
						align-items: center;

						.customerName {}
					}

					.number {
						display: flex;
						align-items: center;
						padding-left: 55upx;
						margin-top: 10upx;

						.input {
							background-color: #f5f5f5;
							border-radius: 10upx;
							width: 100upx;
							text-align: center;
							height: 60upx;
							margin-right: 10upx;
						}
					}

				}
			}


		}

		.images-wrapper {
			background-color: #ffffff;
			margin: 20upx 20upx 0 20upx;
			border-radius: 20upx;
			padding: 25upx;

			.title-wrapper {
				display: flex;
				align-items: center;

				.icon {
					margin-right: 5upx;
				}

				.title {
					height: 34upx;
					line-height: 34upx;
					font-size: 34upx;
					color: #222222;
					padding-left: 10upx;
					border-left: 8upx solid #555BFF;

					&.require::after {
						content: '*';
						color: red;
					}
				}

				.placeholder {
					flex: 1;
					text-align: right;
					font-size: 32upx;
					color: #666666;
				}

				.value {
					flex: 1;
					text-align: right;
					font-size: 32upx;
					color: #212121;
				}

				.small {
					font-size: 28upx;
					color: #898989;
				}

				.input {
					flex: 1;
					text-align: right;
					font-size: 32upx;
					color: #212121;
					margin-right: 10upx;
				}
			}

			.product-image-list {
				display: flex;
				flex-wrap: wrap;
				margin-top: 26upx;

				.item {
					position: relative;
					width: 32%;
					height: 206upx;
					border-radius: 20upx;
					overflow: hidden;
					margin-left: 2%;
					margin-bottom: 20upx;

					&.add-item {
						display: flex;
						align-items: center;
						justify-content: center;
						background-color: #F0F0F0;
					}

					&:nth-child(3n+1) {
						margin-left: 0;
					}

					.product-image {
						width: 100%;
						height: 100%;
					}

					.del-wrapper {
						position: absolute;
						width: 50upx;
						height: 50upx;
						display: flex;
						align-items: center;
						justify-content: center;
						background-color: rgba(0, 0, 0, .5);
						top: 0;
						right: 0;
					}
				}
			}
		}

		.block {
			width: 100%;
			height: 150upx;
		}

		.btn-wrapper {
			position: fixed;
			height: 100upx;
			width: 100%;
			bottom: 0;
			background-color: #ffffff;
			display: flex;
			align-items: center;
			z-index: 1;

			.btn {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				height: 100%;
				background-color: #555BFF;
				color: #ffffff;

				&.default {
					color: #232323;
					background-color: #ffffff;
				}
			}
		}
	}
</style>